package com.chinacolor.manageweb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.chinacolor.commonPojo.TbContentCategoryEntity;
import com.chinacolor.commonService.TbContentCategoryService;
import com.commonsutils.utils.JunUtil;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/manager/contentgory")
public class ContentGoryController {

    @Autowired
    private TbContentCategoryService contentCategoryService;

    /**
     *  广告分类查询
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity contentGoryGet(String id){
        try {
            Integer queryId = 0;
            if(!JunUtil.ifNull(id)){
                if(!JunUtil.isNaN(id)){
                    queryId = JunUtil.parseInt(id);
                }
            }

            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("parent_id",queryId);

            List<TbContentCategoryEntity> resultList = contentCategoryService.list(queryWrapper);

            if(null == resultList || resultList.isEmpty()){
                return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
            }

            return ResponseEntity.status(HttpStatus.OK).body(resultList);
        }catch (Exception e){
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }


    /**
     *  新增节点 并返回该节点信息
     * @param parentId
     * @param name
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<TbContentCategoryEntity> contentGoryPost(String parentId,String name){
        try {
            if(JunUtil.ifNull(parentId) || JunUtil.ifNull(name)){
                if(JunUtil.isNaN(parentId)){
                    return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
                }
            }

            TbContentCategoryEntity contentCategoryEntity = new TbContentCategoryEntity();

            contentCategoryEntity.setName(name);
            contentCategoryEntity.setParentId(Long.parseLong(parentId));
            contentCategoryEntity.setCreated(JunUtil.getCurrentTime());
            contentCategoryEntity.setUpdated(JunUtil.getCurrentTime());
            contentCategoryEntity.setIsParent(false);
            contentCategoryEntity.setStatus(1);
            contentCategoryEntity.setSortOrder(1);

            Boolean fag = contentCategoryService.saveOrUpdate(contentCategoryEntity);

            if(fag){

                TbContentCategoryEntity parent = contentCategoryService.getById(contentCategoryEntity.getParentId());
                if(!parent.getIsParent()){
                    parent.setIsParent(true);
                    Boolean fag1 =contentCategoryService.updateById(parent);
                }

                QueryWrapper<TbContentCategoryEntity> queryWrapper = new QueryWrapper<TbContentCategoryEntity>();
                queryWrapper.setEntity(contentCategoryEntity);
                TbContentCategoryEntity contentCategoryEntity1 = contentCategoryService.getOne(queryWrapper);

                if(null != contentCategoryEntity1){
                    return ResponseEntity.status(HttpStatus.OK).body(contentCategoryEntity1);
                }
            }
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }catch (Exception e){
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     *  节点重命名
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity<Void> contentGoryUpdate(TbContentCategoryEntity contentCategoryEntity){
        try {

            Boolean fag = contentCategoryService.updateById(contentCategoryEntity);

            if(fag){
                return ResponseEntity.status(HttpStatus.OK).build();
            }
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 删除节点
     * @param parentId
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public ResponseEntity<TbContentCategoryEntity> contentGoryDelete(String parentId,String id){
        try{
            if(JunUtil.ifNull(parentId) || JunUtil.ifNull(id)){
                return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
            }

            // 删除以当前节点为父节点的子节点
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("parent_id",Long.parseLong(id));

            Boolean fag = contentCategoryService.remove(updateWrapper);

            //  删除当前节点
            Boolean fag1 = contentCategoryService.removeById(Long.parseLong(id));

            //  查询当前子节点的父节点是否还有子节点，如果没有，则修改，若有，则不修改
            QueryWrapper<TbContentCategoryEntity> queryWrapper = new QueryWrapper<TbContentCategoryEntity>();
            queryWrapper.eq("parent_id",Long.parseLong(parentId));
            List<TbContentCategoryEntity> childrenList = contentCategoryService.list(queryWrapper);

            // 修改其父节点 是否为父节点的属性
            if(childrenList.size()<=0){
                TbContentCategoryEntity tbContentCategoryEntity = new TbContentCategoryEntity();
                tbContentCategoryEntity.setIsParent(false);
                tbContentCategoryEntity.setId(Long.parseLong(parentId));

                Boolean fag2 = contentCategoryService.updateById(tbContentCategoryEntity);
            }

            return ResponseEntity.status(HttpStatus.OK).build();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }


}
